<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/19
  Time: 9:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="<c:url value='/js/jquery-1.8.2.js'/>"></script>
</head>

<body>
    <center>
        <form action="<c:url value='/goodsServlet'/>" method="post">
            <input type="hidden" name="method" value="buy"/>
        <table border="1">
            <tr align="center" bgcolor="gray">
                <td colspan="100">
                    <font size="30px" color="blue">商品购买</font>
                </td>
            </tr>
            <tr>
                <td>商品编号</td>
                <td>${gb.id}
                    <input type="hidden" name="id" value="${gb.id}"/>
                </td>
            </tr>
            <tr>
                <td>商品名称</td>
                <td>${gb.name}
                </td>
            </tr>
            <tr>
                <td>商品分类</td>
                <td>${gb.sname}
                </td>
            </tr>
            <tr>
                <td>单价</td>
                <td><span id="price">${gb.price}</span>
                </td>
            </tr>
            <tr>
                <td>剩余数量</td>
                <td><span id="remaining">${gb.remaining}</span>
                </td>
            </tr>
            <tr>
                <td>购买数量</td>
                <td>
                    <input type="number" name="buycount" onchange="changeprice(this)"/>
                </td>
            </tr>
            <tr>
                <td>总金额(元)</td>
                <td>
                    <span id="totalprice"></span>
                </td>
            </tr>
            <tr align="center">
                <td colspan="100">
                    <input type="button" value="购买" onclick="checkbuy()"/>
                    <a href="<c:url value='/goodsServlet?method=list'/>"><button>返回</button></a>
                </td>
            </tr>
        </table>
        </form>
    </center>
</body>
<script>
    function checkbuy() {
        var buycount=$("[name='buycount']").val();
        buycountreg=/^[1-9]{1}[\d]{0,}$/;
        var remaining = Number($("#remaining").text());
        if(buycountreg.test(buycount)){
            if(remaining>=buycount){
                document.forms[0].submit();
            }else{
                alert("购买数量已经大于库存了");
            }
        }else{
            alert("购买数量不合法");
        }
    }
    function changeprice(obj) {
        var price = $("#price").text();
        $("#totalprice").html((obj.value)*Number(price));
    }
</script>
</html>
